var appPath = getAppPath();	

$(function(){
	
	// 大分类
	$("#queryTmplType").combobox({
		url:appPath+'/itbTg/getTypeForCombo.action?topIds=1,2,3,4,6,7,8,1010,1011,1030&blkFlg=1',
		valueField:'type_id',
		textField:'type_name',
		onSelect:function(selRecd){
			// TODO
		}
	});
	
	// 组织
	$("#queryTgOrg").combobox({
		url:appPath+'/itbDesign/getOrgInfoLst.action',
		valueField:'org_id',
		textField:'name',
		onLoadSuccess:function(){
			var data = $('#queryTgOrg').combobox('getData');
			 $("#queryTgOrg").combobox('select',data[0].org_id);
			 
			 // 加载并选定一个组织后再检索
			 initJspGrid();
		},
		onSelect:function(selRecd){
			// TODO
		}
	});
	
});

// 样版列表初始化
function initJspGrid(){
	
	$('#tmplGrid').datagrid({
	    url:appPath + "/itbDesign/getDesignInfoLst4Review.action",
	    height:'full',
	    //remoteSort:false,
	    striped:true,// 设置行颜色区分
	    fitColumns:true,
	    queryParams: getQueryParam(),
	    singleSelect:true,
	    // If set to true, clicking a checkbox will always select the row. 
	    // If false, selecting a row will not check the checkbox.
	    selectOnCheck:true,
	    // If true, the checkbox is checked/unchecked when the user clicks on a row. 
	    // If false, the checkbox is only checked/unchecked when the user clicks exactly on the checkbox.
	    checkOnSelect:true,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
  	        {field:'thumb',title:'缩略图',width:65,align:'center',sortable:false,
	        	formatter:function(thumb, rowData, rowIndex){
	        		var retUrl = '';
					if (thumb) {
						retUrl = '<a href="' + thumb + '" target="_blank">'
							+ '<div style="display:table;text-align:center;height:110px;width:100%">'
							+ '<span style="display:table-cell;vertical-align:middle">'
							+ '<img src="' +thumb+'?x-oss-process=image/resize,m_lfit,h_120,w_100'
							+ '" style="display:inline;max-height:100px;max-width:100px"></span></div></a>';
					}
					return retUrl;
	        	}
  	        },
			{field:'chkTmpl',title:'审核',width:30,align:'center',sortable:false,
				formatter:function(value,rowData,rowIndex){
					if(Number(rowData.state||'0')!=31) return "N/A";
					var ret = "<a href='javascript:void(0)'><img width='12px' height='12px' style='border:0px' src='"+appPath+"/js/lib/jquery-easyui/themes/icons/pencil.png' title='预览' ";
					ret += " onClick='chkTmpl("+rowData.design_id+","+rowData.type_id+")'/></a>";
					return ret;
				}
			},
	        {field:'design_id',title:'设计ID',width:35,align:'left',sortable:false,
		    	  formatter:function(id, rowData, rowIndex){
		    		  return '<span style="color:red">'+id+'</span>';
		    	  }
		      },
	        {field:'map_id',title:'分页ID',width:35,align:'center',sortable:false,
		    	  formatter:function(mapId, rowData, rowIndex){
		    		  return '<span style="color:green">'+mapId+'</span>';
		    	  }
		      },
	        {field:'size',title:'尺寸',width:60,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  return 'W('+(rowData.width||'N/A')+')*H('+(rowData.height||'N/A')+')';
		    	  }
		      },
		    {field:'top_type',title:'主分类',width:40,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  var tn = getTopTypeNm(value);
		    		  if(!tn || tn.length==0) {
		    			  tn = '<span style="color:red">未知</span>';
		    		  }
		    		  return tn;
		    	  }
		      },
	        {field:'type_id',title:'子分类',width:40,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  return rowData.type_name || '<span style="color:red">未分类</span>';
		    	  }
		      },
	        {field:'order',title:'显示顺',width:35,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  if(!rowData.design_id) return "";
		    		  return "<a href='javascript:void(0)' onClick='editOrder("+rowData.design_id+","+rowData.order+")'>"+rowData.order+"</a>";
		    	  }
		      },
	        {field:'design_name',title:'设计模版名称',width:100,align:'left',sortable:false},
	        {field:'user_id',title:'设计师',width:50,align:'center',sortable:false,
		    	formatter:function(value, rowData, rowIndex){
		    		var ret = '<span style="color:#EEE0E5;font-size:10px;">暂无</span>';
		    		var nickname = rowData.nickname;
		    		if(nickname){
		    			ret = '<span style="color:#4169E1;font-size:10px;">'+nickname+'</span>';
		    		}
		    		return ret;
		    	}
		    },
        	{field:'state',title:'模版状态',width:40,align:'center',sortable:false,
	        	formatter:function(state, rowData, rowIndex){
	        		var s = Number(state||'0');
	        		var nm = '未知';
	        		switch(s){
	        			case 10: nm='制作中';break;
	        			case 20: nm='<span style="color:blue">待审核</span>';break;
	        			case 31: nm='<span style="color:orange">初审OK</span>';break;
	        			case 41: nm='<span style="color:red">初审NG</span>';break;
	        			case 32: nm='<span style="color:green">复审OK</span>';break;
	        			case 42: nm='<span style="color:red">复审NG</span>';break;
	        		}
	        		return nm;
	        	}},
	        {field:'publish',title:'发布状态',width:40,align:'center',sortable:false,
	        	formatter:function(publish, rowData, rowIndex){
	        		if (Number(publish) == 1) return '<span style="color:green">已发布</span>';
	        		return '<span style="color:red">未发布</span>';
	        	}
	        },
	        {field:'create_time',title:'创建时间',width:50,align:'center',sortable:false,
	        	formatter:function(value,rowData,rowIndex){
					return '<span style="display:block;white-space:pre-wrap;">'+value+'</span>';
	        	}
	        },
	        {field:'update_time',title:'更新时间',width:60,align:'center',sortable:false,
	        	formatter:function(value,rowData,rowIndex){
					return '<span style="display:block;white-space:pre-wrap;">'+value+'</span>';
	        	}
	        }
	    ]],
	    hideColumn:['user_type'],
	    //view: imgView,// 自定义View
	    pagination:true,
		rownumbers:true,
		toolbar:[
		{
			text:'复审通过',
			iconCls:'icon-ok',
			handler:function(){
				doChkOK();
			}
		},
		{
			text:'复审打回',
			iconCls:'icon-cancel',
			handler:function(){
				editChkNG();
			}
		}
//		{
//			text:'删除设计模版',
//			iconCls:'icon-cancel',
//			handler:function(){
//				delTmpl();
//			}
//		}
		]
	    
	});

}

function getTopTypeNm(value){
	var tn = '';
	switch(Number(value||'0')){
  	case 1: tn='单页';break;
  	case 2: tn='多页';break;
  	case 3: tn='PPT';break;
  	case 4: tn='印品';break;
  	case 6: tn='社交媒体';break;
  	case 7: tn='电商';break;
  	case 8: tn='微商';break;
  	case 1010: tn='Logo商品';break;
  	case 1011: tn='营销商品';break;
  	case 1030: tn='视频背景';break;
  }
  return tn;
}

function doChkOK(){
	var selectTmpl=$("#tmplGrid").datagrid("getChecked");
	if(!selectTmpl || selectTmpl.length!=1){
		$.messager.alert('提示信息','请选择单个［初审OK］设计模版进行处理！','warning');
		return;
	}
	
	var ids = "";
	var doFlg = false;
	$.each(selectTmpl, function(index, item){
		if(Number(item.state||'0')==31){
			doFlg = true;
			ids = ids + item.design_id + ",";
		}
	});
	
	if(!doFlg){
		$.messager.alert('提示信息','请选择［初审OK］设计模版进行处理！','warning');
		return;
	}
	
	$.messager.confirm("确认",
		"是否确认[审核通过]？<br/><span style='color:red'>#请确保已经过认真检查</span>",
		function (r) {
	        if (r) {
	        	var url = appPath+"/itbDesignWeb/doChkOK.action?ids="+ids;
	        	$.post(url,function(data){
	        		if(data=="ok"){
	        			$.messager.alert('提示信息','设计模版已复审通过！','info');
	        			doQuery();
	        		} else if (data=="ng"){
	        			// TODO
	        			$.messager.alert('提示信息','ng！','warning');
	        		}
	        	});
	        }
		}
	);
}
function doChkNG(){
	var dId = $("#chkDesignId").val();
	var cont = $("#chkDesc").val();
	if(Number(dId||'0')<=0){
		return $.messager.alert('提示信息','模版参数错误！','warning');
	}
	var url = appPath+"/itbDesignWeb/doChkNG.action";
	var param = {'ids':dId, 'chkCont':cont};
	$.post(url,param,function(data){
		if(data=="ok"){
			$.messager.alert('提示信息','设计模版已复审打回！','info',function(){
				$('#chkNgDlg').window('close');
			});
			doQuery();
		}else {
			$.messager.alert('提示信息','内部错误！','warning');
		}
	});
}
function editChkNG(){
	var selectTmpl=$("#tmplGrid").datagrid("getChecked");
	if(!selectTmpl || selectTmpl.length!=1){
		$.messager.alert('提示信息','请选择单个设计模版进行处理！','warning');
		return;
	}
	
	var dId = '';
	var doFlg = false;
	$.each(selectTmpl, function(index, item){
		if(Number(item.state||'0')==31 || Number(item.state||'0')==32){
			doFlg = true;
			dId = item.design_id;
			return false;
		}
	});
	
	if(!doFlg){
		$.messager.alert('提示信息','请选择［审核OK］设计模版进行处理！','warning');
		return;
	}
	
	$("#chkDesignId").val(dId);
	
	$("#chkNgBtn").show();
	$('#chkNgDlg').window({
		title:'复审打回',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#chkNgDlg').window('move',{top:20,left:20});
	$('#chkNgDlg').window('open');
}
function cancelChkNG(){
	$('#chkNgDlg').window('close');
}

/**
 * 评审设计模版
 */
function chkTmpl(designId,typeId){
	// 非加密方式
	//var editorUrl = getDesignServer()+"/design/getWorkEditors?"+"uId="+g_userId+"&dId="+designId;
	// 加密方式
	var editorUrl = getDesignServer()+'/encrypt/encrypt'
		+urlWithPara({'uId':g_userId,'dId':designId,'typeId':typeId,'destination':'/playFormgoods/v1_0_designEdit'});
	window.open(editorUrl);
}


/**
 * 显示顺序
 */
function editOrder(designId, order){
	$("#designId").val(designId);
	$("#order").val(order);
	
	$("#orderBtn").show();
	$('#orderDlg').window({
		title:'显示顺序设定',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#orderDlg').window('move',{top:20,left:20});
	$('#orderDlg').window('open');
}
/**
 * 修改显示顺序
 */
function doUpdOrder(){
	var designId = $("#designId").val();
	var order = $("#order").val();
	if(!order || order.length == 0 || Number(order)>999 || Number(order)<=0){
		$.messager.alert('提示信息','有效数值范围为：1～999','warning');
		return;
	}
	
	var url = appPath+"/itbDesignWeb/updDesignOrder.action?designId="+designId+"&order="+order;
	$.post(url,function(data){
		if(data=="ok"){
			$.messager.alert('提示信息','显示顺序已更新！','info',function(){
				doQuery();
				$('#orderDlg').window('close');
			});
		}else {
			$.messager.alert('提示信息','显示顺序更新失败！','warning');
		}
	});
}
/**
 * 结束显示顺序编辑
 */
function cancelUpdOrder(){
	$('#orderDlg').window('close');
}

/**
 * 获取请求信息
 */
function getQueryParam(){
	var queryObject = {};
	
	var findTmplType =$("#queryTmplType").combobox('getValue');
	var findOrg = $("#queryTgOrg").combobox('getValue');
	var findTmplState = $("#queryTmplState").combobox('getValue');
	var findPubFlg = $("#queryPublish").combobox('getValue');
	var findTmplName=$("#queryTmplName").val();
	
	if (findTmplType) {
		queryObject.findTmplType = findTmplType;// 类型
	}
	if(findOrg&&Number(findOrg||'0')>0) queryObject.findOrg = findOrg;
	if(findTmplState&&Number(findTmplState||'0')>0) queryObject.findTmplState = findTmplState;// 模版状态
	if(findPubFlg&&Number(findPubFlg||'-1')>=0) queryObject.findPubFlg = findPubFlg;// 发布标记
	if(findTmplName&&findTmplName.length>0) queryObject.findTmplName = findTmplName;// 名称
	
	return queryObject;
}

/**
 * 查询数据
 */
function doQuery(startFlg){
	$("#tmplGrid").datagrid('unselectAll');
	if(startFlg==1){
		$("#tmplGrid").datagrid('load', getQueryParam());
	} else {
		$("#tmplGrid").datagrid('reload', getQueryParam());
	}
}

function fmtCreateTime(value, rowData, rowIndex){
	if(!value)return "";
	var s = new Date(value);
	return rowData.create_time=$.DateUtil.format(s,'yyyy/MM/dd hh:mm:ss');
}
